//
//  SearchPhoneVC.m
//  BrushFace
//
//  Created by timer_q on 16/1/22.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "SearchPhoneVC.h"
#import "NavHelper.h"
#import "ContactViewModel.h"
#import <MessageUI/MessageUI.h>
#import "SearchResultVC.h"
#import "AssetsRecordVC.h"
#import "AlertDialogHelper.h"
@interface SearchPhoneVC ()<UITextFieldDelegate,MFMessageComposeViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *searchBackView;
@property (weak, nonatomic) IBOutlet UITextField *searchText;
@property (weak, nonatomic) IBOutlet UIView *searchView;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIButton *searchBtn;
@property (weak, nonatomic) IBOutlet UIView *resultView;
@property (weak, nonatomic) IBOutlet UIButton *resultBtn;

@property (nonatomic,strong)ContactViewModel *reqModel;

@end

@implementation SearchPhoneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navBar.hidden = YES;
    _searchText.delegate = self;
    [_searchText addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    _searchBackView.layer.cornerRadius = 4;
    _searchBackView.clipsToBounds = YES;
    
    _reqModel = [ContactViewModel SceneModel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)cancelBtnClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)resultBtnClick:(id)sender {
    [self showSMSPicker];
}

- (IBAction)searchBtnClick:(id)sender {
    
    if (![self.searchText.text isMobile]) {
        [AlertDialogHelper showToast:@"请输入11位手机号"];
        return;
    }
    
    [_reqModel searchUserWithPhone:self.searchText.text andCB:^(id output) {
        if ([[NSString stringWithFormat:@"%@",output[@"result"]] integerValue]==0) {
            self.resultView.hidden = NO;
        }else{
            self.resultView.hidden = YES;
            SearchResultVC *vc = [[SearchResultVC alloc] init];
            vc.userid = output[@"userid"];
            [vc setHidesBottomBarWhenPushed:YES];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }];
}


#pragma mark - delegate

- (void)textFieldEditChanged:(UITextField *)textField{
    if (textField.text.length>0) {
        _searchView.hidden = NO;
    }else{
        _searchView.hidden = YES;
    }
    _phoneLabel.text = textField.text;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    [self searchBtnClick:nil];
    return YES;
}

#pragma mark - 发短信
- (void)showSMSPicker {
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
    if (messageClass != nil) {
        if ([messageClass canSendText]) {
            [self displaySMSComposerSheet];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"设备没有短信功能" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [alert show];
        }
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"iOS版本过低,iOS4.0以上才支持程序内发送短信" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
        [alert show];
    }
}
- (void)displaySMSComposerSheet {
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    picker.messageComposeDelegate = self;
    NSString *msg = @"我只想任性的借钱给你，送你120元刷脸红包，快来领取吧。http://t.cn/Rb3vvwP";
    picker.body = [[NSString alloc] initWithString:msg];
    NSArray *array = [NSArray arrayWithObjects:self.searchText.text,nil];
    picker.recipients = array;
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    switch (result)
    {
        case MessageComposeResultCancelled:
            //LOG_EXPR(@"Result: SMS sending canceled");
            break;
        case MessageComposeResultSent:
        {
            
        }
            break;
        case MessageComposeResultFailed:
        {
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"发送失败" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            [alert show];
        }
            break;
        default:
            //LOG_EXPR(@"Result: SMS not sent");
            break;
    }
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
@end
